package com.sbr.seb.wicket.page.administration.application;

import java.util.List;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PropertyListView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sbr.seb.entity.livraison.Application;
import com.sbr.seb.entity.livraison.Version;
import com.sbr.seb.planning.AdministrationService;
import com.sbr.seb.planning.dao.ApplicationDao;
import com.sbr.seb.wicket.page.delivery.DeliveryBasePage;

//TODO refactor this class in another package
public class ListVersionsPage extends DeliveryBasePage {

	protected static final Logger logger = LoggerFactory.getLogger(ListVersionsPage.class);
	
	
	@SpringBean
	private ApplicationDao applicationDao;
	
	@SpringBean
	private AdministrationService administrationService;
    
	public ListVersionsPage() {
    	super();
    	
    	
    	
    	LoadableDetachableModel<List<Version>> versionListModel = new LoadableDetachableModel<List<Version>>() {

			private static final long serialVersionUID = 1L;

			@Override
			protected List<Version> load() {
				Application application = getMySession().getSelectedApplication();
		    	
		    	logger.info("application = {}",application);
		    	if (application == null) {
		    		return applicationDao.findAllVersions();
		    	} else {
		    		return applicationDao.findAllVersions(application);
		    	}
			}
    		
		};
    	
    	add(new PropertyListView<Version>("versionsList", versionListModel) {

			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(ListItem<Version> item) {
				item.add(new Label("application.name"));
				item.add(new Label("version"));
			}
    		
    	});
    	
    	add(new VersionForm("editVersionForm"));
		
    }
    
    private class VersionForm extends Form<Version> {
		
		private static final long serialVersionUID = 1L;

		public VersionForm(String id) {
			super(id);
			Version version = new Version();
			version.setApplication(getMySession().getSelectedApplication()==null?new Application(0, "NULL"):getMySession().getSelectedApplication());
			setDefaultModel(new CompoundPropertyModel<Version>(version));
			add(new TextField<String>("version"));
			add(new Label("application.name"));


		}
		
		@Override
		protected void onSubmit() {
			logger.debug("onSubmit called");
			Version version = getModelObject();
			administrationService.addVersion(version);
			setResponsePage(ListVersionsPage.class);
		}	
		
	}
    
    public void setAdministrationService(AdministrationService administrationService) {
		this.administrationService = administrationService;
	}

	public void setApplicationDao(ApplicationDao applicationDao) {
		this.applicationDao = applicationDao;
	}
}
